import Vue from 'vue'
import Router from 'vue-router'
import constantRouter from './modules/constantRouter.js'

// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}
// hack router replace callback
const originalReplace = Router.prototype.push
Router.prototype.replace = function replace(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch((err) => err)
}
Vue.use(Router)

const commonRoutes = [
  {
    path: '/dashboard',
    component: () => import('@/views/dashboard/index.vue'),
    meta: {
      title: '首页',
    },
  },
  {
    path: '/home',
    component: () => import('@/views/layouts/index.vue'),
    meta: {
      title: '工作台',
    },
  },
  {
    path: '/layoutsDetails',
    component: () => import('@/views/layoutsDetails/index.vue'),
    meta: {
      title: '事项详情',
    },
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    meta: {
      title: ' ',
    },
  },
  {
    path: '/task',
    component: () => import('@/views/task'),
    meta: {
      title: '任务管理',
    },
  },
  {
    path: '/taskDetail',
    component: () => import('@/views/task/taskDetail'),
    meta: {
      title: '任务管理',
    },
  },
  {
    path: '/questionEnter',
    component: () => import('@/views/task/questionEnter'),
    meta: {
      title: '任务管理',
    },
  },
  {
    path: '/sample',
    component: () => import('@/views/sample'),
    meta: {
      title: '样品管理',
    },
  },
  {
    path: '/sampleUse',
    component: () => import('@/views/sample/sampleUse'),
    meta: {
      title: '样品管理',
    },
  },
  {
    path: '/returnSample',
    component: () => import('@/views/sample/returnSample'),
    meta: {
      title: '样品管理',
    },
  },
  {
    path: '/mine',
    component: () => import('@/views/mine'),
    meta: {
      title: '我的',
    },
  },
  {
    path: '/remind',
    component: () => import('@/views/mine/remind'),
    meta: {
      title: '我的',
    },
  },
  {
    path: '/mineUser',
    component: () => import('@/views/mine/mineUser'),
    meta: {
      title: '我的',
    },
  },
  {
    path: '/device',
    component: () => import('@/views/device'),
    meta: {
      title: '设备点检',
    },
  },
]

const routes = [...commonRoutes, ...constantRouter]
const createRouter = () =>
  new Router({
    // mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath
    // base: process.env.BASE_URL,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes,
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

router.beforeEach((to, from, next) => {
  var href = window.location.href
  let origin = window.location.origin
  let code = to.query.code
  // hash模式下，跳转的url并不会按我们想的那样，把#拼在index.html后边，而是会加在url的参数的最后，所以这里要做一下判断，如果是从微信跳转来的，就直接带着我需要的参数code跳到登录页
  if (href.includes('com/jili_v2/m/?code')) {
    //url包括 com/?code 证明为从微信登录授权跳转回来的
    window.location.replace(
      origin +
        '/jili_v2/m/#/login?' +
        location.href.split('?')[1].split('#/login')[0]
    ) //拼接跳转

    // if (href.includes("9020/?code")) {
    //   //url包括 com/?code 证明为从微信登录授权跳转回来的
    //   window.location.replace(
    //     origin + "/#/login?" + location.href.split("?")[1].split("#/login")[0]
    //   ); //拼接跳转
  } else {
    if (to.meta.requireAuth) {
      // 这个很重要，我尝试过直接判断路径是否是首页的方式，结果苹果手机没有问题，安卓手机全部都是死循环一直跳到授权登录页。使用to.meta.requireAuth 这样就正常了。
      // if (code && !token && !usertype) {
      //   // 有code且没有token和角色（已授权未登录）
      //   localStorage.setItem("code", code); // 保存code,跳到登录页
      //   next("/login");
      // } else if (!code && token && usertype) {
      //   //已登录，有token
      //   next();
      // } else if (!code && !token && !usertype) {
      //   //未授权未登录，没有token和角色
      //   //获取授权
      //   window.location.href =
      //     "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
      //     appid +
      //     "&redirect_uri=" +
      //     redirect_uri +
      //     "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
      // }
    } else {
      // 当不是首页时，直接进入路由
      next()
    }
  }
  // next();
})
export default router
